import axios from 'axios'
import userUrl from '../../util/url.js'

const url = userUrl + 'api.php/peisong'
const peisong = {
    namespaced: true,
    state: {
        list: [],
        form: {
            name: '',
            price: ''
        },
        modalShow: false,
        idArr: []
    },
    mutations: {
        setModal(state, payload) {
            state.modalShow = payload
        },
        setform(state, payload) {
            state.form = payload
        },
        setidArr(state, payload) {
            let arr = []
            payload.map((item) => {
                arr.push(item.id)
            })
            state.idArr = arr
            // console.log(state.idArr)
        }
    },
    actions: {
        getList({ state, commit, dispatch }, payload) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'get',
                    url,
                    params: { transform: '1', order: 'id,desc' }
                }).then(res => {
                    state.list = res.data.peisong
                    resolve()
                })
            })
        },
        add({ state, commit, dispatch }, payload) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'post',
                    url,
                    data: state.form
                }).then(res => {
                    commit('setModal', false)
                    dispatch('getList')
                    resolve()
                })
            })
        },
        del({ state, commit, dispatch }, payload) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'delete',
                    url: url + '/' + state.idArr,
                }).then(res => {
                    dispatch('getList')
                    resolve()
                })
            })
        }
    }
}
export default peisong